﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Web;

namespace V5_Common {
    /// <summary>
    /// 所有目录操作从根目录开始
    /// </summary>
    public class FsoHelper {
        string m_CharSet = "utf-8";
        /// <summary>
        /// 设置服务器端操作文件的文件编码
        /// </summary>
        public string CharSet {
            get { return m_CharSet; }
            set { m_CharSet = value; }
        }
        private static readonly FsoHelper m_Instance = new FsoHelper();
        /// <summary>
        /// 
        /// </summary>
        public static FsoHelper Instance {
            get { return m_Instance; }
        }
        /// <summary>
        /// 所有目录操作从根目录开始
        /// </summary>
        public FsoHelper() {

        }
        /// <summary>
        /// 
        /// </summary>
        public string PhysicalApplicationPath {
            get { return HttpContext.Current.Request.PhysicalApplicationPath; }
        }
        /// <summary>
        /// 创建目录
        /// </summary>
        /// <param name="dir">目录名称</param>
        public void CreateDir(string dir) {
            if (dir.Length == 0) return;
            if (!Directory.Exists(HttpContext.Current.Request.PhysicalApplicationPath + "\\" + dir))
                Directory.CreateDirectory(HttpContext.Current.Request.PhysicalApplicationPath + "\\" + dir);
        }

        /// <summary>
        /// 删除目录
        /// </summary>
        /// <param name="dir">目录名称</param>
        public void DeleteDir(string dir) {
            DeleteDir(dir, false);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="dir"></param>
        /// <param name="deleteSub"></param>
        public void DeleteDir(string dir, bool deleteSub) {
            if (dir.Length == 0) return;
            if (Directory.Exists(PhysicalApplicationPath + "\\" + dir))
                Directory.Delete(PhysicalApplicationPath + "\\" + dir, deleteSub);
        }
        /// <summary>
        /// 删除文件
        /// </summary>
        /// <param name="file">文件名称</param>
        public void DeleteFile(string file) {
            if (File.Exists(HttpContext.Current.Request.PhysicalApplicationPath + file))
                File.Delete(HttpContext.Current.Request.PhysicalApplicationPath + file);
        }

        /// <summary>
        /// 创建文件
        /// </summary>
        /// <param name="filePath">文件地址</param>
        /// <param name="pagestr">文件内容</param>
        public void CreateFile(string filePath, string pagestr) {
            filePath = filePath.Replace("/", "\\");
            if (filePath.IndexOf("\\") > -1)
                CreateDir(filePath.Substring(0, filePath.LastIndexOf("\\")));
            StreamWriter sw = new StreamWriter(HttpContext.Current.Request.PhysicalApplicationPath + "\\" + filePath, false, Encoding.GetEncoding(this.CharSet));
            sw.Write(pagestr);
            sw.Close();
        }

        /// <summary>
        /// 移动文件
        /// </summary>
        /// <param name="dir1">源目录</param>
        /// <param name="dir2">目标目录</param>
        public void MoveFile(string dir1, string dir2) {
            dir1 = dir1.Replace("/", "\\");
            dir2 = dir2.Replace("/", "\\");
            if (File.Exists(HttpContext.Current.Request.PhysicalApplicationPath + "\\" + dir1))
                File.Move(HttpContext.Current.Request.PhysicalApplicationPath + "\\" + dir1, HttpContext.Current.Request.PhysicalApplicationPath + "\\" + dir2);
        }
        /// <summary>
        /// 获取文件内容
        /// </summary>
        /// <param name="DirFile">文件地址</param>
        /// <returns></returns>
        public string GetFileContent(string DirFile) {
            DirFile = DirFile.Replace("/", "\\");
            if (File.Exists(HttpContext.Current.Request.PhysicalApplicationPath + "\\" + DirFile)) {
                StreamReader sr = new StreamReader(HttpContext.Current.Request.PhysicalApplicationPath + "\\" + DirFile, Encoding.GetEncoding(this.CharSet));
                string Content = sr.ReadToEnd();
                sr.Close();
                return Content;
            }
            return "";
        }
        /// <summary>
        /// 根据时间得到目录名
        /// </summary>
        /// <returns></returns>
        public string GetDateDir() {
            return DateTime.Now.ToString("yyyyMMdd");
        }

        /// <summary>
        /// 根据时间得到文件名
        /// </summary>
        /// <returns></returns>
        public string GetDateFile() {
            return DateTime.Now.ToString("HHmmssff");
        }
        /// <summary>
        /// 复制文件
        /// </summary>
        /// <param name="file1">源文件地址</param>
        /// <param name="file2">目标文件地址</param>
        public void CopyFile(string file1, string file2) {
            file1 = file1.Replace("/", @"\");
            file2 = file2.Replace("/", @"\");
            if (File.Exists(HttpContext.Current.Request.PhysicalApplicationPath + @"\" + file1)) {
                if (File.Exists(HttpContext.Current.Request.PhysicalApplicationPath + @"\" + file2)) {
                    File.Delete(HttpContext.Current.Request.PhysicalApplicationPath + @"\" + file2);
                }
                File.Copy(HttpContext.Current.Request.PhysicalApplicationPath + @"\" + file1, HttpContext.Current.Request.PhysicalApplicationPath + @"\" + file2);
            }
        }
    }
}
